<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\Site as siteModel;
use think\facade\Db;
use think\facade\View;

class Site extends Base{
    // 编辑信息
    public function index($id=0){
		$where[] = ['lang','=',$this->lang];
        $rs = Db::name('site')->where($where)->order('id desc')->find();
        View::assign('rs',$rs);
        
		if(!request()->isPost()){
            // 前台注册会员默认会员组
            $groupWhere[] = ['is_admin','=',0]; $groupWhere[] = ['lang','=',$this->lang];
            View::assign('authgroupList',Db::name('auth_group')->where($groupWhere)->order('sequence desc')->select());

            // 短信验证码模板
            $smsVerifyWhere[] = ['temp_type','=','sms']; $smsVerifyWhere[] = ['lang','=',$this->lang];
            View::assign('smsVerifyTemp',Db::name('send_template')->where($smsVerifyWhere)->order('id desc')->select());

            // 邮件验证码模板
            $emailVerifyWhere[] = ['temp_type','=','email']; $emailVerifyWhere[] = ['lang','=',$this->lang];
            View::assign('emailVerifyTemp',Db::name('send_template')->where($emailVerifyWhere)->order('id desc')->select());

			return view();
        }else{
			try {
				// 判断是新增还是编辑
				$site = Db::name('site')->where('lang',$this->lang)->select();
				$action = !empty($site) ? 'edit' : 'add';
				siteModel::edit($action,input('id'));
				// 如果上传了ico，则删除默认的ico图标
				if($rs['ico'] != input('ico') and file_exists('.'.$rs['ico'])){
				    unlink('.'.$rs['ico']);
				}
                return json(['code' => 200, 'msg' => lang('c_success'), 'data' => '']);
            } catch (Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
            }
        }		
	}
}